package com.shop.controller;

import com.shop.common.SetMeta;
import com.shop.entity.Order;
import com.shop.exception.OrderException;
import com.shop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单数据列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @param user_id
     * @param pay_status
     * @param is_send
     * @param order_fapiao_title
     * @param order_fapiao_company
     * @param order_fapiao_content
     * @param consignee_addr
     * @return
     */
    @PreAuthorize("hasAuthority('155')")
    @GetMapping("/orders")
    public SetMeta selectAll(@RequestParam(required = false) String query,
                             @RequestParam int pagenum, @RequestParam int pagesize,
                             @RequestParam(required = false) Integer user_id,
                             @RequestParam(required = false) String pay_status,
                             @RequestParam(required = false) String is_send,
                             @RequestParam(required = false) String order_fapiao_title,
                             @RequestParam(required = false) String order_fapiao_company,
                             @RequestParam(required = false) String order_fapiao_content,
                             @RequestParam(required = false) String consignee_addr) {
        Order order = new Order();
        order.setUser_id(user_id);
        order.setPay_status(pay_status);
        order.setIs_send(is_send);
        order.setOrder_fapiao_title(order_fapiao_title);
        order.setOrderFapiaoCompany(order_fapiao_company);
        order.setOrder_fapiao_content(order_fapiao_content);
        order.setConsignee_addr(consignee_addr);
        return orderService.selectAll(query, pagenum, pagesize, order);
    }

    /**
     * 修改订单状态
     * @param id
     * @param order
     * @return
     */
    @PreAuthorize("hasAuthority('154')")
    @PutMapping("/orders/{id}")
    public SetMeta updateOrder(@PathVariable Integer id, @RequestBody(required = false) Order order) {
        SetMeta setMeta = new SetMeta();
        try {
            order.setOrderId(id);
            setMeta = orderService.updateOrder(order);
        } catch (OrderException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 查看订单详情
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('155')")
    @GetMapping("/orders/{id}")
    public SetMeta selectOrder(@PathVariable Integer id) {
        SetMeta setMeta = new SetMeta();
        try {
            Order order = orderService.selectOrder(id);
            setMeta.setSuccess(order,"获取成功");
        } catch (OrderException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 修改地址
     * @param id
     * @param order
     * @return
     */
    @PreAuthorize("hasAuthority('154')")
    @PutMapping("/orders/address/{id}")
    public SetMeta updateOrderAddress(@PathVariable Integer id,@RequestBody Order order){
        SetMeta setMeta = new SetMeta();
        try {
            if (order.getConsignee_addr()==null){
                throw new OrderException("地址参数为空");
            }
            setMeta=orderService.updateOrderAddress(id,order.getConsignee_addr());
        } catch (OrderException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    // 109 添加订单
}
